package top.swle.austers.controller;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.google.gson.Gson;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import top.swle.austers.bean.ResponseBean;
import top.swle.austers.bean.User;
import top.swle.austers.jwt.JwtUtil;
import top.swle.austers.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    UserMapper userMapper;


    @PostMapping("/login")
    public ResponseBean login(@RequestBody User user0){

        String token = JwtUtil.createJwt(user0.getPhonenumber());
        User user = userMapper.findByPhonenumber(user0.getPhonenumber());
        if (user == null){
            return new ResponseBean(-1,"用户名不存在",null);
        }else if (user.getPassword().equals(user0.getPassword())){
            return new ResponseBean(1,"登录成功",token);
        }else {
            return new ResponseBean(-2,"密码错误，请重试",null);
        }
    }

    @PostMapping("/signin")
    public ResponseBean signin(@RequestBody User user){
        String phonenumber = user.getPhonenumber();
        User user1 = userMapper.findByPhonenumber(phonenumber);
        if (user1 == null){
            System.out.println("用户为空");
            userMapper.insertByUser(user);
            return new ResponseBean(1,"注册成功",null);
        }else {
            System.out.println("用户已存在");
            return new ResponseBean(-1,"注册失败",null);
        }
    }
//    @RequiresAuthentication
    @PostMapping("/update")
    public ResponseBean update(@RequestBody User user){
        try {
            userMapper.update(user);
            return new ResponseBean(1,"更新成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(-1,"更新失败",null);
        }

    }

    @PostMapping("/get_info")
    public ResponseBean getInfo(@RequestBody User user){
        try {
            user = userMapper.findByPhonenumber(user.getPhonenumber());
            Gson gson = new Gson();
            String json = gson.toJson(user);
            return new ResponseBean(1,"获取成功",json);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseBean(-1,"失败",null);
        }
    }

    //短信验证码
    @PostMapping("/getsms")
    public ResponseBean getCode(@RequestBody User user){
        User user1 = userMapper.findByPhonenumber(user.getPhonenumber());
        if (user1 == null){
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FgE6wL7xfr4vefSSHMi", "1lQm6LGuiB8BpLDbFvxks7yv96aTog");

            IAcsClient client = new DefaultAcsClient(profile);
//        SingleSendMailRequest sendMailRequest = new SingleSendMailRequest();


            //6位随机数生成
            int vcode = 100000+(int)(Math.random()*(999999-100000));

            CommonRequest commonRequest = new CommonRequest();
            commonRequest.setSysMethod(MethodType.POST);
            commonRequest.setSysDomain("dysmsapi.aliyuncs.com");
            commonRequest.setSysVersion("2017-05-25");
            commonRequest.setSysAction("SendSms");
            commonRequest.putQueryParameter("RegionId", "cn-hangzhou");
            commonRequest.putQueryParameter("PhoneNumbers", user.getPhonenumber());
            commonRequest.putQueryParameter("SignName", "Austers");
            commonRequest.putQueryParameter("TemplateCode", "SMS_187571517");
            commonRequest.putQueryParameter("TemplateParam", "{\"code\":"+"\""+vcode+"\"}");

            try {
                CommonResponse commonResponse = client.getCommonResponse(commonRequest);
                System.out.println(commonResponse.getData());
                String code = vcode+"";
                return new ResponseBean(1,"发送成功",code);
            } catch (ClientException e) {
                e.printStackTrace();
                return new ResponseBean(-1,"发送失败",null);

            }
        }else {
            return new ResponseBean(-2,"此号码已注册",null);
        }

    }

    @RequestMapping(path = "/401")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ResponseBean unauthorized() {
        return new ResponseBean(401, "Unauthorized", null);
    }
}
